package com.wan.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * jwt工具类
 * @author xzx
 */
@Slf4j
public class JwtTokenUtils {
    /**
     * 短token过期时间2小时，
     */
    private static final long EXPIRE_TIME = 604800000;
    /**
     * 刷新token过期时间一个周
     */
    private static final long REF_EXPIRE_TIME = 604800000;


    /**
     * token私钥
     */
    private static  final  String TOKEN_SECRET = "123456";


    /**
     * 校验token是否正确
     *
     * @param token 密钥
     * @return 是否正确
     */
    public static boolean verify(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm)
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception exception) {
            log.error(exception.getMessage(),exception);
            return false;
        }
    }


    /**
     * 前端登录生成token
     * @param id
     * @return
     */
    public static String signShow(Integer id,String nickeName) {
        try {
//            过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
//            私钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
//            设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            // 附带username，userId信息，生成签名
            return JWT.create()
                    .withHeader(header)
                    .withClaim("id", id)
                    .withClaim("nickName",nickeName)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public static String signRefShow() {
        try {
//            过期时间
            Date date = new Date(System.currentTimeMillis() + REF_EXPIRE_TIME);
//            私钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
//            设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            // 附带username，userId信息，生成签名
            return JWT.create()
                    .withHeader(header)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }

    }

    public static Integer getOpenId(String token) {
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getClaim("id").asInt();
    }

    public static Date getExpDate(String token){
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getClaim("exp").asDate();
    }



//    /**
//     * 获取用户信息
//     * @param token
//     * @return
//     */
//    public static ShowUserDO getUserInfo(String token) {
//        try {
//            ShowUserDO  showUserDO = new ShowUserDO();
//            DecodedJWT jwt = JWT.decode(token);
//            showUserDO.setUserId(jwt.getClaim("id").asLong());
//            showUserDO.setUsername(jwt.getClaim("username").asString());
//            return showUserDO;
//        } catch (JWTDecodeException e) {
//            return null;
//        }
//    }
}
